var config =require('../config.js');
var AV = require('leanengine');
var log4js = require('log4js');
var logger = log4js.getLogger('normal');
module.exports = function (req, res, next) {
    var url = req.originalUrl;
    if(url.indexOf('/init') == 0){
        next();
        return;
    }
    if(url.indexOf('/foo') == 0){
        next();
        return;
    }
    //过滤微信支付回调的请求
    if(url.indexOf('/wx/pay/notify') == 0){
        next();
        return;
    }
    if(url.indexOf('/wx?back=') == 0){
        next();
        return;
    }
    if(url.indexOf('/wx/callback') == 0){
        next();
        return;
    }
    if(config.dev == 'DEV'){
        if(req.AV.user){
            return next();
        }
        var openid = config.wx.open_id;
        AV.User.logIn(openid, "gr100").then(function(u3){
            //logger.info(u3);
            next();
        }).catch(function(e){
            logger.error(e);
        });
        return;
    }
    if(!req.AV.user){
        return res.redirect("/wx?back="+encodeURIComponent(url));
    }
    res.locals.current_user = req.AV.user;
    next();
};